package org.dromara.hm.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.hm.domain.bo.HmOrderTransferHistoryBo;
import org.dromara.hm.domain.vo.HmOrderTransferHistoryVo;
import org.dromara.hm.domain.HmOrderTransferHistory;
import org.dromara.hm.mapper.HmOrderTransferHistoryMapper;
import org.dromara.hm.service.IHmOrderTransferHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单归属转移历史Service业务层处理
 *
 * @author Hzy
 * @date 2025-06-13
 */
@RequiredArgsConstructor
@Service
public class HmOrderTransferHistoryServiceImpl implements IHmOrderTransferHistoryService {

    private final HmOrderTransferHistoryMapper baseMapper;

    /**
     * 查询订单归属转移历史
     *
     * @param orderTransferHistoryId 主键
     * @return 订单归属转移历史
     */
    @Override
    public HmOrderTransferHistoryVo queryById(Long orderTransferHistoryId){
        return baseMapper.selectVoById(orderTransferHistoryId);
    }

    /**
     * 分页查询订单归属转移历史列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 订单归属转移历史分页列表
     */
    @Override
    public TableDataInfo<HmOrderTransferHistoryVo> queryPageList(HmOrderTransferHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HmOrderTransferHistory> lqw = buildQueryWrapper(bo);
        Page<HmOrderTransferHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的订单归属转移历史列表
     *
     * @param bo 查询条件
     * @return 订单归属转移历史列表
     */
    @Override
    public List<HmOrderTransferHistoryVo> queryList(HmOrderTransferHistoryBo bo) {
        LambdaQueryWrapper<HmOrderTransferHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HmOrderTransferHistory> buildQueryWrapper(HmOrderTransferHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HmOrderTransferHistory> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HmOrderTransferHistory::getOrderTransferHistoryId);
        lqw.eq(bo.getCustomerTransferHistoryId() != null, HmOrderTransferHistory::getCustomerTransferHistoryId, bo.getCustomerTransferHistoryId());
        lqw.eq(bo.getSaleOrderId() != null, HmOrderTransferHistory::getSaleOrderId, bo.getSaleOrderId());
        lqw.eq(bo.getCustomerId() != null, HmOrderTransferHistory::getCustomerId, bo.getCustomerId());
        lqw.eq(bo.getOldSaleUser() != null, HmOrderTransferHistory::getOldSaleUser, bo.getOldSaleUser());
        lqw.like(StringUtils.isNotBlank(bo.getOldSaleUserName()), HmOrderTransferHistory::getOldSaleUserName, bo.getOldSaleUserName());
        lqw.eq(bo.getNewSaleUser() != null, HmOrderTransferHistory::getNewSaleUser, bo.getNewSaleUser());
        lqw.like(StringUtils.isNotBlank(bo.getNewSaleUserName()), HmOrderTransferHistory::getNewSaleUserName, bo.getNewSaleUserName());
        lqw.eq(bo.getOldSaleDept() != null, HmOrderTransferHistory::getOldSaleDept, bo.getOldSaleDept());
        lqw.like(StringUtils.isNotBlank(bo.getOldSaleDeptName()), HmOrderTransferHistory::getOldSaleDeptName, bo.getOldSaleDeptName());
        lqw.eq(bo.getNewSaleDept() != null, HmOrderTransferHistory::getNewSaleDept, bo.getNewSaleDept());
        lqw.like(StringUtils.isNotBlank(bo.getNewSaleDeptName()), HmOrderTransferHistory::getNewSaleDeptName, bo.getNewSaleDeptName());
        lqw.eq(bo.getOperatorId() != null, HmOrderTransferHistory::getOperatorId, bo.getOperatorId());
        lqw.like(StringUtils.isNotBlank(bo.getOperatorName()), HmOrderTransferHistory::getOperatorName, bo.getOperatorName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), HmOrderTransferHistory::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增订单归属转移历史
     *
     * @param bo 订单归属转移历史
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HmOrderTransferHistoryBo bo) {
        HmOrderTransferHistory add = MapstructUtils.convert(bo, HmOrderTransferHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOrderTransferHistoryId(add.getOrderTransferHistoryId());
        }
        return flag;
    }

    /**
     * 修改订单归属转移历史
     *
     * @param bo 订单归属转移历史
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HmOrderTransferHistoryBo bo) {
        HmOrderTransferHistory update = MapstructUtils.convert(bo, HmOrderTransferHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HmOrderTransferHistory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除订单归属转移历史信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public void batchInsertHistories(List<HmOrderTransferHistory> orderHistoryList)
    {
        baseMapper.insertBatch(orderHistoryList);
    }
}
